package com.sinosoft.emplmis.application.web;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.emplmis.application.web.dto.RoleData;
import com.sinosoft.emplmis.application.web.dto.UserData;
import com.sinosoft.emplmis.application.web.dto.UserOrgsData;
import com.sinosoft.framework.core.api.domain.UserOrgData;
import com.sinosoft.framework.core.api.domain.RoleEntity;
import com.sinosoft.framework.core.api.domain.UserEntity;
import com.sinosoft.framework.core.api.service.RoleService;
import com.sinosoft.framework.core.api.service.UserOrgService;
import com.sinosoft.framework.core.api.service.UserService;
import com.sinosoft.framework.core.common.utils.ResultJson;
import com.sinosoft.framework.core.common.utils.StringUtil;

/**
 * 用户信息控制器
 * @author xiaoxiong
 *
 */
@Controller
@RequestMapping("application/user")
public class UserController {

	private Logger logger = LoggerFactory.getLogger(UserController.class.getName());
	
	@Autowired
	private UserService userService;

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserOrgService userOrgService;
	
	/**
	 * 获取用户信息
	 * @param loginName
	 * @return
	 */
	@RequestMapping(value="/getUserContext",method = RequestMethod.GET)
	public @ResponseBody UserData getUser(String loginName) {
		if(StringUtil.isBlank(loginName)) {
			return null;
		}
		UserData userData = new UserData();
		List<RoleData> roleDatas = new ArrayList<RoleData>();
		logger.info("调用服务：getUser开始");
		UserEntity userEntity = userService.queryUserInfoByLoginName(loginName);
		UserOrgData userOrgData = userOrgService.getUserOrgByUserName(loginName);
		List<RoleEntity> roleEntitys = roleService.queryRolesByUserId(userEntity.getId());
		for(RoleEntity roleEntity : roleEntitys) {
			RoleData roleData = new RoleData();
			roleData.toRoleData(roleData, roleEntity);
			roleDatas.add(roleData);
		}
		UserOrgsData userOrgsData = new UserOrgsData();
		userOrgsData.toUserOrgsData(userOrgsData, userOrgData);
		userData.toUserData(userData, userEntity, roleDatas, userOrgsData);
		logger.info("调用服务：getUser结束");
		return userData;
	}
	
	/**
	 * 更新密码
	 * @param userData
	 * @return
	 */
	@RequestMapping(value="/updatePassword",method = RequestMethod.POST)
	@ResponseBody
	public ResultJson updatePassword(UserData userData)
	{
		ResultJson resultJson = new ResultJson();
		if(null == userData)
		{
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
        	resultJson.setMessage("没有用户信息");
        	resultJson.setResults(null);
		}else
		{
			if(StringUtil.isBlank(userData.getLoginName()))
			{
				resultJson.setSuccess(ResultJson.SUCCESS_NO);
	        	resultJson.setMessage("用户名称为空！");
	        	resultJson.setResults(null);
			}else
			{
				UserEntity userEntity = userService.queryUserInfoByLoginName(userData.getLoginName());
				if(null != userEntity)
				{
					if(userService.checkPassword(userEntity, userData.getCurrentPwd()))
					{
						//设置新密码
						userEntity.setPlainPassword(userData.getNewPwd());
						//加密处理
						userService.entryptPassword(userEntity);
						//更新密码
						userService.updateUserInfoByLoginName(userEntity);
						resultJson.setSuccess(ResultJson.SUCCESS_YES);
			        	resultJson.setMessage("修改成功！");
			        	resultJson.setResults(null);
					}else
					{
						resultJson.setSuccess(ResultJson.SUCCESS_NO);
			        	resultJson.setMessage("原密码错误");
			        	resultJson.setResults(null);
					}
				}
			}
		}
		return resultJson;
	}
}
